package com.example.yrdev.ordering_system.Model;

import android.content.Context;
import android.content.SharedPreferences;

import com.example.yrdev.ordering_system.Utils.GlobalFunction;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import javax.microedition.khronos.opengles.GL;

/**
 * Created by luke on 19/12/2016.
 */

public class FixPriceTimeSchedule {//定额信息

//    public static   final String DH_TABLE_NAME = "FixPriceTimeSchedule";
//    public static final String DH_timeInterval="timeInterval";
//    public static final String DH_cardID="cardID";
//    public static final String DH_timeType="timeType";
//
//    public  static final String DH_CREATE_TABLE="CREATE TABLE " + DH_TABLE_NAME + "( " + DH_cardID + " TEXT," + DH_timeInterval + " TEXT,"
//            +DH_timeType + " TEXT" +  ")";


   private static  FixPriceTimeSchedule fixPriceTimeSchedule;

    public static FixPriceTimeSchedule getInstance(){

        if (fixPriceTimeSchedule==null){

            fixPriceTimeSchedule=new FixPriceTimeSchedule();

        }

        return fixPriceTimeSchedule;

    }

    public ArrayList<FixPriceTimeSchedule>fixPriceTimeScheduleArrayList;



   public HashMap<String,Object>timePriceSchedule;

   public String cardID;

    public FixPriceTimeSchedule(){

        timePriceSchedule=new HashMap<>();
        fixPriceTimeScheduleArrayList=new ArrayList<>();

    }

    public FixPriceTimeSchedule getCorrespondingObj(String cardID){
        FixPriceTimeSchedule temp=new FixPriceTimeSchedule();
       for (FixPriceTimeSchedule schedule : fixPriceTimeScheduleArrayList){
           if (schedule.cardID.equals(cardID)){
               temp=schedule;

           }
       }
       return temp;

    }


    public  Integer getCurrentFixPrice(String cardID){

         Double price=0.0;
        for (FixPriceTimeSchedule schedule : fixPriceTimeScheduleArrayList){

            if (schedule.cardID.equals(cardID)){

                for (String key : schedule.timePriceSchedule.keySet()){
                    String[]keyArr=key.split("-");
                    String initialTime=keyArr[0]+":00";

                    String endTime=keyArr[1]+":00";
                    String currentTime= GlobalFunction.getCurrentHourMin();



                    try {
                        boolean stat= GlobalFunction.isTimeBetweenTwoTime(initialTime,endTime);

                        if (stat){

                            Object object=schedule.timePriceSchedule.get(key);

                            price=Double.valueOf(object.toString());


                            break;
                        }

                    }catch (Exception e){

                        GlobalFunction.printTextByTag(e.getMessage());
                    }



                }

            }

        }

        return GlobalFunction.convertToInt(price*100);

    }


    public  String getTime(String cardType){
        String time="";

        Double price=0.0;

        FixPriceTimeSchedule fixPriceTimeSchedule=new FixPriceTimeSchedule();
        fixPriceTimeSchedule.cardID="1";
        fixPriceTimeSchedule.timePriceSchedule.put("00:00-06:00","breakfast");
        fixPriceTimeSchedule.timePriceSchedule.put("06:00-12:00","lunch");
        fixPriceTimeSchedule.timePriceSchedule.put("12:00-18:00","dinner");
        fixPriceTimeSchedule.timePriceSchedule.put("18:00-23:00","midnight");

        fixPriceTimeScheduleArrayList.add(fixPriceTimeSchedule);

        for (FixPriceTimeSchedule schedule : fixPriceTimeScheduleArrayList){



                for (String key : schedule.timePriceSchedule.keySet()){
                    String[]keyArr=key.split("-");
                    String initialTime=keyArr[0]+":00";

                    String endTime=keyArr[1]+":00";



                    try {
                        boolean stat= GlobalFunction.isTimeBetweenTwoTime(initialTime,endTime);

                        if (stat){

                        String object= schedule.timePriceSchedule.get(key).toString();

                         GlobalFunction.printTextByTag("object:"+object);
                         time=object;


                            break;
                        }

                    }catch (Exception e){

                        GlobalFunction.printTextByTag(e.getMessage());
                    }



                }



        }

        return time;

    }

    public void init(){

        SharedPreferences preferences=GlobalFunction.context.getSharedPreferences("FixPriceTimeSchedule", Context.MODE_PRIVATE);
        String json=preferences.getString("timeSechdule","");
        Gson gson = new Gson();

        Set<FixPriceTimeSchedule> set= gson.fromJson(json,Set.class);
        fixPriceTimeScheduleArrayList=new ArrayList<>(set);


    }
    public void saveSchedule() throws IOException {

        SharedPreferences preferences=GlobalFunction.context.getSharedPreferences("FixPriceTimeSchedule", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=preferences.edit();

        Set<FixPriceTimeSchedule> set=new HashSet<FixPriceTimeSchedule>();
        set.addAll(fixPriceTimeScheduleArrayList);


        Gson gson = new Gson();
        String json=gson.toJson(set);
        editor.putString("timeSechdule",json);

        editor.apply();





    }



}
